﻿#region Using Statements
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using BreakOutBattles.Game.Events;
using BreakOutBattles.Game.Physics;
using BreakOutBattles.Sound;
#endregion

namespace BreakOutBattles.Game.Items
{
    /// <summary>
    /// A base class for all bricks. Bricks are game items that can be destroyed by the player
    /// when something hits it. A brick can also be used as a default brick.
    /// </summary>
    public class Brick : GameItem, ISolidObject
    {
        #region Delegates and Events
        public delegate void BrickDestroyedEventHandler(object sender, BrickDestroyedArgs args);
        public event BrickDestroyedEventHandler BrickDestroyedEvent;
        #endregion

        #region Fields
        private int resistance = 1;
        protected int destructionBonus = 10;
        #endregion

        public Brick(Vector2 location, Texture2D itemTexture)
            : base(location, itemTexture)
        { }
                
        /// <summary>
        /// Fires an event indicating that this brick has recently been destroyed.
        /// </summary>
        private void FireOnDestroyedEvent()
        {
            if (BrickDestroyedEvent != null)
            {
                BrickDestroyedEvent(this, new BrickDestroyedArgs(destructionBonus));
            }
        }

        /// <summary>
        /// Gets and sets the number of hits this brick is able to handle before 
        /// it breaks.
        /// </summary>
        public int Resistance
        {
            get
            {
                return resistance;
            }
            set
            {
                resistance = value;

                if (resistance <= 0)
                {
                    active = false;
                    FireOnDestroyedEvent();
                }
            }
        }

        /// <summary>
        /// Updates the brick.
        /// </summary>
        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);
            locked = false;
        }

        #region ISolidObject Members
        /// <summary>
        /// Gets the collision rectangle of this brick.
        /// </summary>
        public virtual Rectangle CollisionRect
        {
            get 
            {
                collisionRect.X = (int)currentLocation.X;
                collisionRect.Y = (int)currentLocation.Y;

                return collisionRect;
            }
            set
            {
                collisionRect = value;
            }
        }

        /// <summary>
        /// Gets and sets a boolean indicating if the brick is in motion or not.
        /// </summary>
        public virtual bool OnMotion
        {
            get
            {
                return false;
            }
        }

        /// <summary>
        /// A function that is called every time any game item collides with this brick.
        /// </summary>
        public void RegisterCollision()
        {
            SoundManager.PlayEffect(SoundEffects.BRICK_HIT_1);

            if (!locked)
            {
                Resistance--;
                locked = true;
            }
        }
        #endregion
    }
}
